<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>美食日记 - 美食社区</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.2/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" th:href="@{/css/style.css}">
    <style>
        .post-card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            height: 100%;
            border-radius: 10px;
            overflow: hidden;
        }

        .post-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .post-img {
            height: 200px;
            overflow: hidden;
        }

        .post-img img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.3s ease;
        }

        .post-card:hover .post-img img {
            transform: scale(1.1);
        }

        .post-meta {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        .post-author-avatar {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            margin-right: 10px;
            object-fit: cover;
        }

        .post-author {
            font-size: 14px;
            color: #6c757d;
        }

        .post-date {
            font-size: 12px;
            color: #adb5bd;
            margin-left: auto;
        }

        .post-stats {
            display: flex;
            justify-content: space-between;
            color: #6c757d;
            font-size: 14px;
        }

        .post-category {
            background-color: #fff3e6;
            color: #fd7e14;
            border: 1px solid #ffd7b5;
            border-radius: 20px;
            padding: 3px 10px;
            font-size: 12px;
            display: inline-block;
        }

        .btn-primary {
            background-color: #fd7e14;
            border-color: #fd7e14;
        }

        .btn-primary:hover {
            background-color: #e76b00;
            border-color: #e76b00;
        }

        .filter-section {
            background-color: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 30px;
        }
    </style>
</head>

<body>
    <!-- 导航栏 -->
    <nav th:replace="~{common/header :: header}"></nav>

    <div class="container mt-5 pt-5">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2 class="section-title">美食日记</h2>
            <a href="/posts/create" class="btn btn-primary" th:if="${session.user != null}">
                <i class="fas fa-plus me-2"></i>发布日记
            </a>
        </div>

        <!-- 筛选区域 -->
        <div class="filter-section">
            <form id="filterForm" method="get" action="/posts">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label class="form-label">分类</label>
                        <select class="form-select" name="category">
                            <option value="">全部分类</option>
                            <option th:each="cat : ${categories}" th:value="${cat.name}" th:text="${cat.name}"
                                th:selected="${category == cat.name}">分类名称</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">排序方式</label>
                        <select class="form-select" name="sort">
                            <option value="latest" th:selected="${sort == 'latest'}">最新发布</option>
                            <option value="popular" th:selected="${sort == 'popular'}">最受欢迎</option>
                            <option value="comments" th:selected="${sort == 'comments'}">评论最多</option>
                        </select>
                    </div>
                    <div class="col-md-4 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-search me-2"></i>筛选
                        </button>
                    </div>
                </div>
            </form>
        </div>

        <!-- 文章列表 -->
        <div class="row g-4">
            <div class="col-md-4 mb-4" th:each="post : ${posts}">
                <div class="post-card card">
                    <div class="post-img">
                        <img th:src="${post.firstImage}" class="card-img-top" th:alt="${post.title}">
                    </div>
                    <div class="card-body">
                        <div class="post-meta">
                            <img th:src="${post.authorAvatar}" class="post-author-avatar" alt="作者头像">
                            <span class="post-author" th:text="${post.authorName}">作者名</span>
                            <span class="post-date"
                                th:text="${#dates.format(post.createTime, 'yyyy-MM-dd')}">发布日期</span>
                        </div>
                        <h5 class="card-title" th:text="${post.title}">文章标题</h5>
                        <p class="card-text" th:text="${post.summary}">文章摘要...</p>
                        <div class="d-flex justify-content-between align-items-center mt-3">
                            <span class="post-category"
                                th:text="${post.categoryName != null ? post.categoryName : '未分类'}">分类名称</span>
                            <div class="post-stats">
                                <span class="me-3"><i class="fas fa-heart text-danger me-1"></i><span
                                        th:text="${post.likes}">123</span></span>
                                <span><i class="fas fa-eye text-primary me-1"></i><span
                                        th:text="${post.views}">456</span></span>
                            </div>
                        </div>
                    </div>
                    <div class="card-footer bg-white">
                        <a th:href="@{'/post/' + ${post.id}}" class="btn btn-outline-primary w-100">
                            <i class="fas fa-book-open me-2"></i>阅读全文
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- 分页 -->
        <nav aria-label="Page navigation" class="mt-4" th:if="${totalPages > 1}">
            <ul class="pagination justify-content-center">
                <li class="page-item" th:classappend="${currentPage == 1 ? 'disabled' : ''}">
                    <a class="page-link"
                        th:href="@{/posts(page=${currentPage - 1}, category=${param.category}, sort=${param.sort})}"
                        aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                <li class="page-item" th:each="i : ${#numbers.sequence(1, totalPages)}"
                    th:classappend="${i == currentPage ? 'active' : ''}">
                    <a class="page-link" th:href="@{/posts(page=${i}, category=${param.category}, sort=${param.sort})}"
                        th:text="${i}"></a>
                </li>
                <li class="page-item" th:classappend="${currentPage == totalPages ? 'disabled' : ''}">
                    <a class="page-link"
                        th:href="@{/posts(page=${currentPage + 1}, category=${param.category}, sort=${param.sort})}"
                        aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </ul>
        </nav>
    </div>

    <!-- Bootstrap Bundle with Popper -->
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/js/bootstrap.bundle.min.js"></script>
    <!-- jQuery -->
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <!-- 退出登录脚本 -->
    <script th:replace="~{common/header :: logout-script}"></script>
    <script>
        $(document).ready(function () {
            // 设置筛选表单的初始值
            const urlParams = new URLSearchParams(window.location.search);
            $('select[name="category"]').val(urlParams.get('category') || '');
            $('select[name="sort"]').val(urlParams.get('sort') || 'latest');
        });
    </script>
</body>

</html>